package com.aplink.generic.webservice;

import java.util.ArrayList;

import android.location.Location;
import android.os.AsyncTask;
import android.text.TextUtils;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.cache.ContentDataManager;
import com.aplink.generic.cache.MasterConfigManager;
import com.aplink.generic.constant.WebServiceContant;
import com.aplink.generic.constant.type.ActionType;
import com.aplink.generic.constant.type.ListViewSelection;
import com.aplink.generic.database.SqlMethods;
import com.aplink.generic.network.GenericFile;
import com.aplink.generic.pojo.ContentData;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.MasterConfig;
import com.aplink.generic.pojo.ObjPropInfo;
import com.aplink.generic.pojo.TableSyncData;
import com.aplink.generic.sharepreference.GenericSharePreference;
import com.aplink.generic.util.LogUtil;
import com.aplink.generic.util.ResourcesHelper;
import com.aplink.generic.widgets.ApLinkGallery;
import com.aplink.generic.widgets.ApLinkListView;
import com.aplink.sg.R;

public class GenericWebService {
	public static GenericWebService newInstance() {
		final GenericWebService genericWebService = new GenericWebService(null);
		return genericWebService;
	}

	public static GenericWebService newInstance(
	        final GenericFragment genericFragment) {
		final GenericWebService genericWebService = new GenericWebService(
		        genericFragment);
		return genericWebService;
	}
	private final GenericFragment mGenericFragment;

	private ActionType mTaskType;

	private final ArrayList<HttpURL> webserviceURLs;

	private GenericWebService(final GenericFragment genericFragment) {
		this.mGenericFragment = genericFragment;
		this.webserviceURLs = new ArrayList<HttpURL>();
	}

	private HttpURL baseUrl(final ContentData objPropCol,
	        final GenericObject genericObject, final String table,
	        final String action) {
		final MasterConfig masterConfig = MasterConfigManager.getInstance()
		        .get(GenericSharePreference.getInstance()
		                .getMasterConfigFileID());
		final String domain = masterConfig.getWebserviceConfig()
		        .getWebserviceDomain();
		final String webserviceFunction = objPropCol.getWebserviceFunction();
		final HttpURL genericWebserviceURL = new HttpURL();
		genericWebserviceURL.setDomain(domain).setContentData(objPropCol)
		        .setWebserviceFunction(webserviceFunction)
		        .setMethod(WebServiceContant.GET).setAction(action)
		        .setTable(table);
		if (action.equals(WebServiceContant.ACTION_INSERT)) {
			genericWebserviceURL.setJsonValue(genericObject);
		} else if (action.equals(WebServiceContant.ACTION_UPDATE)) {
			final ObjPropInfo objPropInfo = objPropCol.getCreatedTime(table);
			if (objPropInfo != null) {
				final String columName = objPropCol.getCreatedTimeObject()
				        .get(0).getFullColumnName();
				final String value = genericObject.getValue(columName)
				        .toString();
				if (!TextUtils.isEmpty(value)) {
					genericWebserviceURL.setJsonCondition(value);
					genericWebserviceURL.setJsonValue(genericObject);
				}
			}
		} else {
			final ObjPropInfo objPropInfo = objPropCol.getCreatedTime(table);
			if (objPropInfo != null) {
				final String columName = objPropCol.getCreatedTimeObject()
				        .get(0).getFullColumnName();
				final String value = genericObject.getValue(columName)
				        .toString();
				genericWebserviceURL.setJsonCondition(value);
			}
		}
		return genericWebserviceURL;
	}

	public GenericWebService delete() {
		this.mTaskType = ActionType.DELETE;
		final ContentData objPropCol = this.mGenericFragment.getContentData();
		final GenericObject genericObject = this.mGenericFragment
		        .getSelectedObject();
		final String table = objPropCol.getMainTable();
		final HttpURL httpURL = baseUrl(objPropCol, genericObject, table,
		        "delete");
		this.webserviceURLs.add(httpURL);
		return this;
	}

	private GenericWebService delete(final ContentData objPropCol,
	        final GenericObject genericObject, final String table) {
		final HttpURL httpURL = baseUrl(objPropCol, genericObject, table,
		        WebServiceContant.ACTION_DELETE);
		this.webserviceURLs.add(httpURL);
		return this;
	}

	public GenericWebService download() {
		final MasterConfig masterConfig = MasterConfigManager.getInstance()
		        .get(GenericSharePreference.getInstance()
		                .getMasterConfigFileID());
		this.mTaskType = ActionType.DOWNLOAD;
		final ContentData objPropCol = this.mGenericFragment.getContentData();
		for (int i = 0; i < objPropCol.getRefreshTable().size(); i++) {
			String table = objPropCol.getRefreshTable().valueAt(i);
			if (masterConfig.getWebserviceConfig().getTableInitLists()
			        .contains(table)) {
				download(table, masterConfig);
			} else if (masterConfig.getWebserviceConfig().getTableSyncData()
			        .containsKey(table)) {
				TableSyncData tableSyncData = masterConfig
				        .getWebserviceConfig().getTableSyncData().get(table);
				download(tableSyncData, masterConfig);
			}
		}
		return this;
	}

	private GenericWebService download(String table, MasterConfig masterConfig) {
		final HttpURL url = new HttpURL();
		url.setDomain(masterConfig.getWebserviceConfig().getWebserviceDomain())
		        .setWebserviceFunction(
		                masterConfig.getWebserviceConfig().getInitData())
		        .setTable(table);
		this.webserviceURLs.add(url);
		return this;
	}

	private GenericWebService download(TableSyncData tableSyncData,
	        MasterConfig masterConfig) {
		final int contentDataFileId = ResourcesHelper
		        .getRawIdByName(tableSyncData.getWebserviceFunction());
		if (contentDataFileId != ResourcesHelper.RESOURCE_NOT_FOUND) {
			final ContentData objPropCol = ContentDataManager.getInstance()
			        .get(contentDataFileId);
			final HttpURL httpURL = new HttpURL();
			final String webServiceDomain = masterConfig.getWebserviceConfig()
			        .getWebserviceDomain();
			httpURL.setDomain(webServiceDomain).setContentData(objPropCol)
			        .setWebserviceFunction(objPropCol.getWebserviceFunction())
			        .setTable(tableSyncData.getTableName()).setAction("load")
			        .setHasDownloadFile(tableSyncData.isHavePhoto());
			if (httpURL.isHasDownloadFile()) {
				if (objPropCol.getImageLocalPath() != null) {
					httpURL.setFileObjPropInfo(objPropCol.getImageLocalPath());
				}
			}
			this.webserviceURLs.add(httpURL);
		}
		return this;
	}

	public GenericWebService downloadAll() {
		final MasterConfig masterConfig = MasterConfigManager.getInstance()
		        .get(GenericSharePreference.getInstance()
		                .getMasterConfigFileID());
		this.mTaskType = ActionType.DOWNLOAD_ALL;
		for (int i = 0; i < masterConfig.getWebserviceConfig()
		        .getTableSyncData().size(); i++) {
			final TableSyncData tableSyncData = masterConfig
			        .getWebserviceConfig().getTableSyncData().valueAt(i);
			final int contentDataFileId = tableSyncData
			        .getWebserviceFunctionID();
			final ContentData objPropCol = ContentDataManager.getInstance()
			        .get(contentDataFileId);

			final HttpURL httpURL = new HttpURL();
			final String webServiceDomain = masterConfig.getWebserviceConfig()
			        .getWebserviceDomain();
			httpURL.setDomain(webServiceDomain).setContentData(objPropCol)
			        .setWebserviceFunction(objPropCol.getWebserviceFunction())
			        .setTable(tableSyncData.getTableName()).setAction("load")
			        .setHasDownloadFile(tableSyncData.isHavePhoto());
			if (httpURL.isHasDownloadFile()) {
				for (int j = 0; j < objPropCol.size(); j++) {
					final ObjPropInfo objPropInfo = objPropCol.valueAt(j);
					if (objPropInfo.isImageLocalPath()) {
						httpURL.setFileObjPropInfo(objPropInfo);
						break;
					}
				}
			}
			this.webserviceURLs.add(httpURL);
		}
		return this;
	}

	public void excute() {
		final HttpURL[] urls = this.webserviceURLs
		        .toArray(new HttpURL[this.webserviceURLs.size()]);
		new HttpTask(this.mGenericFragment, this.mTaskType).executeOnExecutor(
		        AsyncTask.THREAD_POOL_EXECUTOR, urls);
	}

	public GenericWebService forceUpload() {
		this.mTaskType = ActionType.UPLOAD;
		final MasterConfig masterConfig = MasterConfigManager.getInstance()
		        .get();
		for (int i = 0; i < masterConfig.getWebserviceConfig()
		        .getTableSyncData().size(); i++) {
			final TableSyncData tableSyncData = masterConfig
			        .getWebserviceConfig().getTableSyncData().valueAt(i);
			final int contentDataFileId = tableSyncData
			        .getWebserviceFunctionID();
			final ContentData objPropCol = ContentDataManager.getInstance()
			        .get(contentDataFileId);
			final ArrayList<GenericObject> distinctObjectList = this.mGenericFragment
			        .getDatabaseGet().selectDistinct(
			                tableSyncData.getTableName(), objPropCol, null);
			for (final GenericObject object : distinctObjectList) {
				this.delete(objPropCol, object, tableSyncData.getTableName());
				this.upload(object, objPropCol, tableSyncData.isHavePhoto());
			}
		}
		return this;
	}

	public GenericWebService forgotPassword() {
		this.mTaskType = ActionType.FORGOT_PASSWORD;
		final GenericObject genericObject = this.mGenericFragment
		        .getSelectedObject();
		final ContentData objPropCol = this.mGenericFragment.getContentData();
		final MasterConfig masterConfig = MasterConfigManager.getInstance()
		        .get();
		final String domain = masterConfig.getWebserviceConfig()
		        .getWebserviceDomain();
		final HttpURL httpURL = new HttpURL();
		httpURL.setDomain(domain)
		        .setWebserviceFunction(
		                masterConfig.getWebserviceConfig().getForgotPassword())
		        .setContentData(objPropCol).setEmail(genericObject)
		        .setMethod(WebServiceContant.GET);
		this.webserviceURLs.add(httpURL);
		return this;
	}

	public GenericWebService initializeData() {
		this.mTaskType = ActionType.INIT_DATA;
		final MasterConfig masterConfig = MasterConfigManager.getInstance()
		        .get();

		for (String tableInit : masterConfig.getWebserviceConfig()
		        .getTableInitLists()) {
			final HttpURL url = new HttpURL();
			url.setDomain(
			        masterConfig.getWebserviceConfig().getWebserviceDomain())
			        .setWebserviceFunction(
			                masterConfig.getWebserviceConfig().getInitData())
			        .setTable(tableInit);
			this.webserviceURLs.add(url);
		}
		for (int i = 0; i < masterConfig.getWebserviceConfig()
		        .getTableSyncData().size(); i++) {
			final TableSyncData tableSyncData = masterConfig
			        .getWebserviceConfig().getTableSyncData().valueAt(i);

			final int contentDataFileId = ResourcesHelper
			        .getRawIdByName(tableSyncData.getWebserviceFunction());
			if (contentDataFileId != ResourcesHelper.RESOURCE_NOT_FOUND) {
				final ContentData objPropCol = ContentDataManager.getInstance()
				        .get(contentDataFileId);
				final HttpURL httpURL = new HttpURL();
				final String webServiceDomain = masterConfig
				        .getWebserviceConfig().getWebserviceDomain();
				httpURL.setDomain(webServiceDomain)
				        .setContentData(objPropCol)
				        .setWebserviceFunction(
				                objPropCol.getWebserviceFunction())
				        .setTable(tableSyncData.getTableName())
				        .setAction("load")
				        .setHasDownloadFile(tableSyncData.isHavePhoto());
				if (httpURL.isHasDownloadFile()) {
					if (objPropCol.getImageLocalPath() != null) {
						httpURL.setFileObjPropInfo(objPropCol
						        .getImageLocalPath());
					}
				}
				this.webserviceURLs.add(httpURL);
			}
		}
		return this;
	}

	public GenericWebService insert() {
		this.mTaskType = ActionType.INSERT;
		HttpURL httpURL = null;
		final ContentData objPropCol = this.mGenericFragment.getContentData();
		final GenericObject genericObject = this.mGenericFragment
		        .getSelectedObject();
		final String table = objPropCol.getMainTable();
		if (this.mGenericFragment.getFormDesign().isHasGallery()) {
			ApLinkGallery gallery = (ApLinkGallery) this.mGenericFragment
			        .getAPLinkViewByID(this.mGenericFragment.getFormDesign()
			                .getGalleryID());
			httpURL = new HttpURL();
			httpURL.setContentData(objPropCol)
			        .setDomain(
			                MasterConfigManager.getInstance().get()
			                        .getWebserviceConfig()
			                        .getWebserviceDomain())
			        .setWebserviceFunction(
			                objPropCol.getWebserviceFunction().replace(
			                        "DataHandle?", "ImageDataHandle?"))
			        .setTable(table).setAction(WebServiceContant.ACTION_INSERT)
			        .setJsonValue(genericObject)
			        .setMethod(WebServiceContant.POST)
			        .setListUploadFiles(gallery.getInsertFiles());
		} else {
			httpURL = baseUrl(objPropCol, genericObject, table,
			        WebServiceContant.ACTION_INSERT);
		}
		this.webserviceURLs.add(httpURL);
		return this;
	}

	public GenericWebService login() {
		this.mTaskType = ActionType.LOGIN;
		final ContentData objPropCol = this.mGenericFragment.getContentData();
		final GenericObject genericObject = this.mGenericFragment
		        .getMappingObject();
		final MasterConfig masterConfig = MasterConfigManager.getInstance()
		        .get();
		final String domain = masterConfig.getWebserviceConfig()
		        .getWebserviceDomain();
		final String webserviceFunction = objPropCol.getWebserviceFunction();
		final HttpURL httpURL = new HttpURL();
		httpURL.setContentData(objPropCol).setDomain(domain)
		        .setWebserviceFunction(webserviceFunction)
		        .setJsonValue(genericObject);
		this.webserviceURLs.add(httpURL);
		return this;
	}

	public GenericWebService save() {
		this.mTaskType = ActionType.SAVE;
		final ContentData objPropCol = this.mGenericFragment.getContentData();
		final GenericObject genericObject = this.mGenericFragment
		        .getSelectedObject();
		final String table = objPropCol.getMainTable();
		final ObjPropInfo primaryKey = objPropCol.getPrimaryKey(table);
		boolean isUpdate = false;
		if (TextUtils.isEmpty(genericObject.getValue(primaryKey
		        .getFullColumnName()))) {
			isUpdate = false;
		} else {
			isUpdate = this.mGenericFragment.getDatabaseGet().isExists(
			        genericObject, objPropCol);
		}
		HttpURL httpURL = null;
		if (isUpdate) {
			if (this.mGenericFragment.getFormDesign().isHasGallery()) {
				final ApLinkGallery gallery = (ApLinkGallery) this.mGenericFragment
				        .getAPLinkViewByID(this.mGenericFragment
				                .getFormDesign().getGalleryID());
				final MasterConfig masterConfig = MasterConfigManager
				        .getInstance().get();
				final String domain = masterConfig.getWebserviceConfig()
				        .getWebserviceDomain();
				httpURL = new HttpURL();
				httpURL.setContentData(objPropCol)
				        .setDomain(domain)
				        .setWebserviceFunction(
				                objPropCol.getWebserviceFunction().replace(
				                        "DataHandle?", "ImageDataHandle?"))
				        .setTable(table).setAction("update")
				        .setJsonValue(genericObject)
				        .setMethod(WebServiceContant.POST)
				        .setListUploadFiles(gallery.getInsertFiles())
				        .setListDeleteFiles(gallery.getDeleteFiles());
				final ObjPropInfo objPropInfo = objPropCol
				        .getCreatedTime(table);
				if (objPropInfo != null) {
					final String columName = objPropCol.getCreatedTimeObject()
					        .get(0).getFullColumnName();
					final String value = genericObject.getValue(columName)
					        .toString();
					httpURL.setJsonCondition(value);
				}
				this.webserviceURLs.add(httpURL);
			} else if (this.mGenericFragment.getFormDesign().isHasListView()) {
				final ApLinkListView apLinkListView = (ApLinkListView) this.mGenericFragment
				        .getAPLinkViewByID(this.mGenericFragment
				                .getFormDesign().getListViewID());
				if (apLinkListView.getListViewSelection() != null) {
					if (apLinkListView.getListViewSelection().equals(
					        ListViewSelection.MULTIPLE_SELECTION)) {
						for (final GenericObject object : apLinkListView
						        .getDeleteList()) {
							final HttpURL url = baseUrl(objPropCol, object,
							        table, WebServiceContant.ACTION_DELETE);
							this.webserviceURLs.add(url);
						}

						for (final GenericObject object : apLinkListView
						        .getInsertList()) {
							final HttpURL url = baseUrl(objPropCol, object,
							        table, WebServiceContant.ACTION_INSERT);
							this.webserviceURLs.add(url);
						}
					} else if (apLinkListView.getListViewSelection().equals(
					        ListViewSelection.SINGLE_SELECTION)) {
						for (final GenericObject object : apLinkListView
						        .getInsertList()) {
							final HttpURL url = baseUrl(objPropCol, object,
							        table, WebServiceContant.ACTION_UPDATE);
							this.webserviceURLs.add(url);
						}
					}
				}

			} else {
				httpURL = baseUrl(objPropCol, genericObject, table,
				        WebServiceContant.ACTION_UPDATE);
				this.webserviceURLs.add(httpURL);
			}
		} else {
			if (this.mGenericFragment.getFormDesign().isHasGallery()) {
				final ApLinkGallery gallery = (ApLinkGallery) this.mGenericFragment
				        .getAPLinkViewByID(this.mGenericFragment
				                .getFormDesign().getGalleryID());
				final MasterConfig masterConfig = MasterConfigManager
				        .getInstance().get();
				final String domain = masterConfig.getWebserviceConfig()
				        .getWebserviceDomain();
				httpURL = new HttpURL();
				httpURL.setContentData(objPropCol)
				        .setDomain(domain)
				        .setWebserviceFunction(
				                objPropCol.getWebserviceFunction().replace(
				                        "DataHandle?", "ImageDataHandle?"))
				        .setTable(table).setAction("insert")
				        .setJsonValue(genericObject)
				        .setMethod(WebServiceContant.POST)
				        .setListUploadFiles(gallery.getInsertFiles());
				this.webserviceURLs.add(httpURL);
			} else if (this.mGenericFragment.getFormDesign().isHasListView()) {
				final ApLinkListView apLinkListView = (ApLinkListView) this.mGenericFragment
				        .getAPLinkViewByID(this.mGenericFragment
				                .getFormDesign().getListViewID());
				for (final GenericObject object : apLinkListView
				        .getInsertList()) {
					final HttpURL url = baseUrl(objPropCol, object, table,
					        WebServiceContant.ACTION_INSERT);
					this.webserviceURLs.add(url);
				}
			} else {
				httpURL = baseUrl(objPropCol, genericObject, table,
				        WebServiceContant.ACTION_INSERT);
				this.webserviceURLs.add(httpURL);
			}
		}
		return this;
	}

	public GenericWebService update() {
		final ContentData objPropCol = this.mGenericFragment.getContentData();
		final GenericObject genericObject = this.mGenericFragment
		        .getMappingObject();
		update(objPropCol, genericObject, true);
		return this;
	}

	private GenericWebService update(ContentData objPropCol,
	        GenericObject genericObject, boolean isCheckGallery) {
		this.mTaskType = ActionType.UPDATE;
		final String table = objPropCol.getMainTable();
		HttpURL httpURL = null;
		if (isCheckGallery
		        && this.mGenericFragment.getFormDesign().isHasGallery()) {
			final ApLinkGallery gallery = (ApLinkGallery) this.mGenericFragment
			        .getAPLinkViewByID(this.mGenericFragment.getFormDesign()
			                .getGalleryID());
			final MasterConfig masterConfig = MasterConfigManager.getInstance()
			        .get();
			final String domain = masterConfig.getWebserviceConfig()
			        .getWebserviceDomain();
			httpURL = new HttpURL();
			httpURL.setContentData(objPropCol)
			        .setDomain(domain)
			        .setWebserviceFunction(
			                objPropCol.getWebserviceFunction().replace(
			                        "DataHandle?", "ImageDataHandle?"))
			        .setTable(table).setAction(WebServiceContant.ACTION_UPDATE)
			        .setJsonValue(genericObject)
			        .setMethod(WebServiceContant.POST)
			        .setListUploadFiles(gallery.getInsertFiles())
			        .setListDeleteFiles(gallery.getDeleteFiles());
			final ObjPropInfo objPropInfo = objPropCol.getCreatedTime(table);
			if (objPropInfo != null) {
				final String columName = objPropCol.getCreatedTimeObject()
				        .get(0).getFullColumnName();
				final String value = genericObject.getValue(columName)
				        .toString();
				httpURL.setJsonCondition(value);
			}
		} else {
			httpURL = baseUrl(objPropCol, genericObject, table,
			        WebServiceContant.ACTION_UPDATE);
		}
		this.webserviceURLs.add(httpURL);
		return this;
	}

	public GenericWebService updateLocation(Location location) {
		mTaskType = ActionType.UPDATE_LOCATION;
		final MasterConfig masterConfig = MasterConfigManager.getInstance()
		        .get();
		final String domain = masterConfig.getWebserviceConfig()
		        .getWebserviceDomain();
		final String webserviceFunction = masterConfig.getWebserviceConfig()
		        .getUpdateLocation();
		HttpURL httpURL = new HttpURL();

		ContentData contentData = ContentDataManager.getInstance().get(
		        R.raw.update_location);
		GenericObject genericObject = new GenericObject();
		genericObject.put("Location.Latitude", location.getLatitude() + "");
		genericObject.put("Location.Longitude", location.getLongitude() + "");

		httpURL.setDomain(domain).setWebserviceFunction(webserviceFunction)
		        .setToken(GenericSharePreference.getInstance().getToken())
		        .setContentData(contentData).setJsonValue(genericObject)
		        .setMethod(WebServiceContant.GET);
		this.webserviceURLs.add(httpURL);
		return this;
	}

	public GenericWebService upload() {
		this.mTaskType = ActionType.UPLOAD;

		MasterConfig masterConfig = MasterConfigManager.getInstance().get();
		for (int i = 0; i < masterConfig.getWebserviceConfig()
		        .getTableSyncData().size(); i++) {
			TableSyncData tableSyncData = masterConfig.getWebserviceConfig()
			        .getTableSyncData().valueAt(i);
			if (tableSyncData.isDownloadOnly()) {
				continue;
			}
			String mainTableName = tableSyncData.getTableName();

			int contentDataFileId = tableSyncData.getWebserviceFunctionID();
			ContentData objPropCol = ContentDataManager.getInstance().get(
			        contentDataFileId);
			String syncStatusColumn = objPropCol.getSyncStatus(
			        tableSyncData.getTableName()).getFullColumnName();

			SqlMethods sqlMethods = SqlMethods.getInstance().isNotNull(
			        syncStatusColumn);

			if (tableSyncData.isHavePhoto()) {
				ObjPropInfo objPrimaryKey = objPropCol
				        .getPrimaryKey(mainTableName);
				ObjPropInfo imageLocalPath = objPropCol.getImageLocalPath();

				String distinctColumn = objPrimaryKey.getFullColumnName();
				ArrayList<GenericObject> distinctObjectList = this.mGenericFragment
				        .getDatabaseGet().selectDistinct(
				                tableSyncData.getTableName(), objPropCol,
				                sqlMethods.toString());

				for (GenericObject distinctObject : distinctObjectList) {
					String action = distinctObject.getValue(syncStatusColumn)
					        .toString();
					HttpURL httpURL = new HttpURL();
					httpURL.setContentData(objPropCol)
					        .setDomain(
					                masterConfig.getWebserviceConfig()
					                        .getWebserviceDomain())
					        .setTable(tableSyncData.getTableName())
					        .setAction(action).setJsonValue(distinctObject);

					if (!objPropCol.getCreatedTimeObject().isEmpty()) {
						if (!action.equalsIgnoreCase("insert")) {
							httpURL.setJsonCondition(distinctObject.getValue(
							        objPropCol.getCreatedTime(mainTableName)
							                .getFullColumnName()).toString());
						}
					} else {
						LogUtil.error(LogUtil.TAG_WEBSERVICE,
						        "Created time is required for upload",
						        "Upload", getClass());
					}

					if (action.equalsIgnoreCase("delete")) {
						httpURL.setWebserviceFunction(objPropCol
						        .getWebserviceFunction().replace(
						                "ImageDataHandle?", "DataHandle?"));
						httpURL.setMethod(WebServiceContant.GET);
					} else {
						httpURL.setWebserviceFunction(objPropCol
						        .getWebserviceFunction().replace("DataHandle?",
						                "ImageDataHandle?"));

						final String value = distinctObject.getValue(
						        distinctColumn).toString();

						final SqlMethods whereClause = SqlMethods
						        .getInstance()
						        .equal(objPrimaryKey.getFullColumnName(), value);

						final ArrayList<GenericFile> listUploadFiles = new ArrayList<GenericFile>();
						final ArrayList<GenericFile> listDeleteFile = new ArrayList<GenericFile>();

						final ObjPropInfo fileSyncStatus = objPropCol
						        .getSyncStatus(imageLocalPath.getTable());

						whereClause.and().isNotNull(
						        fileSyncStatus.getFullColumnName());
						final ArrayList<GenericObject> images = this.mGenericFragment
						        .getDatabaseGet().getList(objPropCol,
						                whereClause.toString());

						for (final GenericObject image : images) {
							final String syncStatus = image.getValue(
							        fileSyncStatus.getFullColumnName())
							        .toString();

							final String localPath = image.getValue(
							        imageLocalPath.getFullColumnName())
							        .toString();
							final GenericFile file = new GenericFile();
							file.setLocalPath(localPath);
							file.setFileNameByURl(localPath);
							if (syncStatus.equalsIgnoreCase("insert")) {
								listUploadFiles.add(file);
							} else {
								listDeleteFile.add(file);
							}
						}

						httpURL.setListUploadFiles(listUploadFiles);
						httpURL.setListDeleteFiles(listDeleteFile);
						httpURL.setMethod(WebServiceContant.POST);
					}
					this.webserviceURLs.add(httpURL);
				}
			} else {
				final ArrayList<GenericObject> genericObjects = this.mGenericFragment
				        .getDatabaseGet().getList(objPropCol,
				                sqlMethods.toString());
				for (final GenericObject obj : genericObjects) {
					final String action = obj.getValue(syncStatusColumn)
					        .toString();
					final String domain = masterConfig.getWebserviceConfig()
					        .getWebserviceDomain();
					final String webserviceFunction = objPropCol
					        .getWebserviceFunction();
					final HttpURL httpURL = new HttpURL();
					httpURL.setContentData(objPropCol).setDomain(domain)
					        .setWebserviceFunction(webserviceFunction)
					        .setTable(tableSyncData.getTableName())
					        .setAction(action).setJsonValue(obj);
					if (!objPropCol.getCreatedTimeObject().isEmpty()) {
						if (!action.equalsIgnoreCase("insert")) {
							final String columName = objPropCol
							        .getCreatedTimeObject().get(0)
							        .getFullColumnName();
							final String value = obj.getValue(columName)
							        .toString();
							httpURL.setJsonCondition(value);
						}
					}
					httpURL.setMethod(WebServiceContant.GET);
					this.webserviceURLs.add(httpURL);
				}
			}
		}
		return this;
	}

	private void upload(final GenericObject distinctObject,
	        final ContentData objPropCol, final boolean isHavePhoto) {
		final String mainTableName = objPropCol.getMainTable();
		final ObjPropInfo objPrimaryKey = objPropCol
		        .getPrimaryKey(mainTableName);
		final ObjPropInfo imageLocalPath = objPropCol.getImageLocalPath();
		final String distinctColumn = objPrimaryKey.getFullColumnName();
		final MasterConfig masterConfig = MasterConfigManager.getInstance()
		        .get();
		final String syncStatusColumn = objPropCol.getSyncStatus(mainTableName)
		        .getFullColumnName();
		if (isHavePhoto) {
			final String action = distinctObject.getValue(syncStatusColumn)
			        .toString();
			final HttpURL httpURL = new HttpURL();
			httpURL.setContentData(objPropCol)
			        .setDomain(
			                masterConfig.getWebserviceConfig()
			                        .getWebserviceDomain())
			        .setTable(mainTableName).setAction(action)
			        .setJsonValue(distinctObject);
			if (!objPropCol.getCreatedTimeObject().isEmpty()) {
				if (!action.equalsIgnoreCase("insert")) {
					final String columName = objPropCol.getCreatedTime(
					        mainTableName).getFullColumnName();
					final String value = distinctObject.getValue(columName)
					        .toString();
					httpURL.setJsonCondition(value);
				}
			} else {
				LogUtil.error(LogUtil.TAG_WEBSERVICE,
				        "Created time is required for upload", "Upload",
				        getClass());
				throw new NullPointerException(
				        "Created time is required for upload");
			}

			if (action.equalsIgnoreCase("delete")) {
				httpURL.setWebserviceFunction(objPropCol
				        .getWebserviceFunction().replace("ImageDataHandle?",
				                "DataHandle?"));
				httpURL.setMethod(WebServiceContant.GET);
			} else {
				httpURL.setWebserviceFunction(objPropCol
				        .getWebserviceFunction().replace("DataHandle?",
				                "ImageDataHandle?"));

				final String value = distinctObject.getValue(distinctColumn)
				        .toString();

				final SqlMethods whereClause = SqlMethods.getInstance().equal(
				        objPrimaryKey.getFullColumnName(), value);

				final ArrayList<GenericFile> listUploadFiles = new ArrayList<GenericFile>();
				final ArrayList<GenericFile> listDeleteFile = new ArrayList<GenericFile>();

				final ObjPropInfo fileSyncStatus = objPropCol
				        .getSyncStatus(imageLocalPath.getTable());

				whereClause.and().isNotNull(fileSyncStatus.getFullColumnName());
				final ArrayList<GenericObject> images = this.mGenericFragment
				        .getDatabaseGet().getList(objPropCol,
				                whereClause.toString());

				for (final GenericObject image : images) {
					final String syncStatus = image.getValue(
					        fileSyncStatus.getFullColumnName()).toString();

					final String localPath = image.getValue(
					        imageLocalPath.getFullColumnName()).toString();
					final GenericFile file = new GenericFile();
					file.setLocalPath(localPath);
					file.setFileNameByURl(localPath);
					if (syncStatus.equalsIgnoreCase("insert")) {
						listUploadFiles.add(file);
					} else {
						listDeleteFile.add(file);
					}
				}

				httpURL.setListUploadFiles(listUploadFiles);
				httpURL.setListDeleteFiles(listDeleteFile);
				httpURL.setMethod(WebServiceContant.POST);
			}
			this.webserviceURLs.add(httpURL);
		} else {
			final String action = distinctObject.getValue(syncStatusColumn)
			        .toString();
			final String domain = masterConfig.getWebserviceConfig()
			        .getWebserviceDomain();
			final String webserviceFunction = objPropCol
			        .getWebserviceFunction();
			final HttpURL httpURL = new HttpURL();
			httpURL.setContentData(objPropCol).setDomain(domain)
			        .setWebserviceFunction(webserviceFunction)
			        .setTable(mainTableName).setAction(action)
			        .setJsonValue(distinctObject);
			if (!objPropCol.getCreatedTimeObject().isEmpty()) {
				if (!action.equalsIgnoreCase("insert")) {
					final String columName = objPropCol.getCreatedTimeObject()
					        .get(0).getFullColumnName();
					final String value = distinctObject.getValue(columName)
					        .toString();
					httpURL.setJsonCondition(value);
				}
			}
			httpURL.setMethod(WebServiceContant.GET);
			this.webserviceURLs.add(httpURL);
		}
	}
}
